package top.pmwly.oauth2.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author Herther
 * @version 1.0.0
 * @ClassName SpringSecurityConfig.java
 * @Description 客户端security配置类
 * @createTime 2021年08月22日 15:04:00
 */
@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    /**
     * 注入 PasswordEncoder
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }


    /**
     * 重写 configure
     * @Pram HttpSecurity
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //关闭crs
        http.csrf().disable()
                //这些请求不需要拦截，不需要认证
                .authorizeRequests()
                .antMatchers("/login/**","/logout/**","/auth/**")
                .permitAll()
                //其他全部拦截需要认证
                .anyRequest()
                .authenticated()
                //连接符
                .and()
                //登录表单放行
                .formLogin().permitAll();
    }
    /**
     * 注入 认证管理器
     */
    @Bean
    public AuthenticationManager authenticationManager1() throws Exception {
        return super.authenticationManager();
    }

}
